package com.fingard.dsp.bank.directbank.xib01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by dalt on 2017/7/18.
 */
public class XIB01Transaction extends XIB01Base implements ITransaction {
    //采用流处理方式
    //1、循环调用readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用writeRetDetail()，写入返回ATS的明细
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans Resp = new RetAtsTrans(reqHeader);

        Resp.writeStart();
        String txnCode = "IESI109013";
        String clientReq = writeHeader(txnCode);

        String accountNo = p_req7516.actNum;
        String startDate = p_req7516.dateFrom;
        String endDate = p_req7516.dateTo;
        String pageNo = "1";
        String pageSize = "99999999";

        clientReq += "{\"accountNo\":\"" + accountNo + "\",";
        clientReq += "\"startDate\":\"" + startDate + "\",";
        clientReq += "\"endDate\":\"" + endDate + "\",";
        clientReq += "\"pageNo\":\"" + pageNo + "\",";
        clientReq += "\"pageSize\":\"" + pageSize + "\"}";

        clientReq += "}";

        String sign = Signatures(clientReq);
        String bankRet = postData(sign, bankFront.serverIP, bankFront.serverPort);

        JSONObject bankResp = JSON.parseObject(bankRet);
        String retCode = bankResp.getString("respCode");
        String respMsg = bankResp.getString("respMsg");
        if (!IsResultValid(retCode, respMsg)) {
            Resp.writeRespCode("-1");
            Resp.writeRespInfo(respMsg);
            Resp.writeEnd();
            return Resp;
        }


        com.alibaba.fastjson.JSONObject body = bankResp.getJSONObject("body");
        List<Object> list = body.getJSONArray("acctTranDetailList");
        if (list == null || list.size() == 0) {
            Resp.writeRespCode("0");
            Resp.writeRespInfo("无银行流水返回！");
            Resp.writeEnd();
            return Resp;
        }

        Resp.writeRespCode("0");
        Resp.writeRespInfo(respMsg);
        for (Object iter : list) {
            JSONObject i = (JSONObject) iter;
            RetAtsTransDetail detail = new RetAtsTransDetail();
            detail.actNum = p_req7516.actNum;
            detail.recAccNum = i.getString("cbsToAcctNo");
            detail.accBal = i.getString("closingBal");
            detail.recAccName = i.getString("cbsToAcctName");
            detail.tranDate = com.fingard.constant.Format.Date10Format.format(com.fingard.constant.Format.Date8Format.parse(i.getString("tradeDate")));
            detail.tranTime = com.fingard.constant.Format.DateTimeFormat.format(com.fingard.constant.Format.Date8Format.parse(i.getString("tradeDate")));
            detail.valueDate = com.fingard.constant.Format.Date10Format.format(com.fingard.constant.Format.Date8Format.parse(i.getString("tradeDate")));
            //modified by dalt on 2018/3/20
            //.net版本的交易金额判断是取非空方，但是据我观察测试数据，实际数据借贷两方金额均非空，即一者为0
            //故java版这边逻辑修改如下
            //detail.tranAmt = StringUtils.isEmpty(i.getString("creditAmt")) ? i.getString("creditAmt") : i.getString("debitAmt");
            BigDecimal creditAmt = new BigDecimal(StringHelper.defaultIfEmpty(i.getString("creditAmt"), "0").replaceAll("-", ""));
            BigDecimal debitAmt = new BigDecimal(StringHelper.defaultIfEmpty(i.getString("debitAmt"), "0").replaceAll("-", ""));
            boolean income = creditAmt.signum() > 0 ? false : true;
            detail.usage = i.getString("remarks");
            detail.tranAmt = (income ? debitAmt : creditAmt).toPlainString();
            detail.originator = i.getString("bankSerialNo");
            detail.bnkSeqNo = i.getString("bankSerialNo");
            detail.cdFlag = income ? "2" : "1";
            Resp.writeRetDetail(detail);
        }
        Resp.writeEnd();
        return Resp;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        RetAtsTrans Resp = new RetAtsTrans(reqHeader);
        Resp.writeStart();
        String txnCode = "IESI109012";
        String clientReq = writeHeader(txnCode);

        String accountNo = p_req7515.actNum;
        String queryDate = Format.Date8Format.format(new Date());
        String pageNo = "1";
        String pageSize = "99999999";

        clientReq += "{\"accountNo\":\"" + accountNo + "\",";
        clientReq += "\"queryDate\":\"" + queryDate + "\",";
        clientReq += "\"pageNo\":\"" + pageNo + "\",";
        clientReq += "\"pageSize\":\"" + pageSize + "\"}";

        clientReq += "}";

        String sign = Signatures(clientReq);
        String bankRet = postData(sign, bankFront.serverIP, bankFront.serverPort);

        JSONObject bankResp = JSON.parseObject(bankRet);
        String retCode = bankResp.getString("respCode");
        String respMsg = bankResp.getString("respMsg");
        if (!IsResultValid(retCode, respMsg)) {
            Resp.writeRespCode("-1");
            Resp.writeRespInfo(respMsg);
            Resp.writeEnd();
            return Resp;
        }

        com.alibaba.fastjson.JSONObject body = bankResp.getJSONObject("body");
        List<Object> list = body.getJSONArray("acctTranDetailList");
        if (list == null || list.size() == 0) {
            Resp.writeRespCode("0");
            Resp.writeRespInfo("无银行流水返回！");
            Resp.writeEnd();
            return Resp;
        }

        Resp.writeRespCode("0");
        Resp.writeRespInfo(respMsg);
        for (Object iter : list) {
            JSONObject i = (JSONObject) iter;
            RetAtsTransDetail detail = new RetAtsTransDetail();
            detail.actNum = p_req7515.actNum;
            detail.recAccNum = i.getString("cbsToAcctNo");
            detail.accBal = i.getString("closingBal");
            detail.recAccName = i.getString("cbsToAcctName");
            detail.tranDate = com.fingard.constant.Format.Date10Format.format(com.fingard.constant.Format.Date8Format.parse(i.getString("tradeDate")));
            detail.tranTime = com.fingard.constant.Format.DateTimeFormat.format(com.fingard.constant.Format.Date8Format.parse(i.getString("tradeDate")));
            detail.valueDate = com.fingard.constant.Format.Date10Format.format(com.fingard.constant.Format.Date8Format.parse(i.getString("tradeDate")));
            //modified by dalt on 2018/3/20
            //.net版本的交易金额判断是取非空方，但是据我观察测试数据，实际数据借贷两方金额均非空，即一者为0
            //故java版这边逻辑修改如下
            //detail.tranAmt = StringUtils.isEmpty(i.getString("creditAmt")) ? i.getString("creditAmt") : i.getString("debitAmt");
            BigDecimal creditAmt = new BigDecimal(StringHelper.defaultIfEmpty(i.getString("creditAmt"), "0").replaceAll("-", ""));
            BigDecimal debitAmt = new BigDecimal(StringHelper.defaultIfEmpty(i.getString("debitAmt"), "0").replaceAll("-", ""));
            boolean income = creditAmt.signum() > 0 ? false : true;
            detail.usage = i.getString("remarks");
            detail.tranAmt = (income ? debitAmt : creditAmt).toPlainString();
            detail.originator = i.getString("bankSerialNo");
            detail.cdFlag = income ? "2" : "1";
            Resp.writeRetDetail(detail);
        }

        Resp.writeEnd();
        return Resp;
    }
}
